Page({
  data: {
    menuItems: [
      { icon: "/pages/index/image/my-order.png", label: "我的订单" },
      { icon: "/pages/index/image/my-coupons.png", label: "我的卡券" },
      // { icon: '/pages/index/image/work-book.png', label: '课评/作业' },
      // { icon: '/pages/index/image/my-timetable.png', label: '我的课表' },
      // { icon: '/pages/index/image/my-points.png', label: '我的积分' },
      // { icon: '/pages/index/image/my-evaluation.png', label: '我的评价' },
      // { icon: '/pages/index/image/my-site.png', label: '已约场地' },
      // { icon: '/pages/index/image/my-reservation.png', label: '我的预约' },
      // { icon: '/pages/index/image/my-coupons.png', label: '我的卡券' }
    ],
    myname: "点击登录",
    grade: "无",
    url: "/pages/index/image/header.png",
    isLoggedIn: false, // 是否已登录
    userInfo: null, // 用户信息
  },

  onLoad: function () {
    // 检查是否已登录，但不强制登录
    this.checkLoginStatus();
  },

  onShow: function () {
    // 每次显示页面时重新检查登录状态
    this.checkLoginStatus();
  },

  // 检查登录状态方法
  checkLoginStatus: function () {
    const app = getApp();
    const loginStatus = app.checkLoginStatus();

    if (loginStatus.isLoggedIn) {
      // 已登录，显示用户信息
      const userInfo = loginStatus.userInfo;
      this.setData({
        isLoggedIn: true,
        userInfo: userInfo,
        myname: userInfo.nickName || userInfo.name || "用户",
        url: userInfo.avatarUrl || "/pages/index/image/header.png",
      });
    } else {
      // 未登录，显示默认状态
      this.setData({
        isLoggedIn: false,
        userInfo: null,
        myname: "点击登录",
        url: "/pages/index/image/header.png",
      });
    }
  },

  // 点击用户头像区域
  handleUserProfileTap: function () {
    // 如果未登录，跳转到登录页面
    if (!this.data.isLoggedIn) {
      wx.navigateTo({
        url: "/pages/registration/registration",
      });
    }
  },

  // 退出登录
  handleLogout: function () {
    wx.showModal({
      title: "退出登录",
      content: "确定要退出登录吗？",
      success: (res) => {
        if (res.confirm) {
          // 清除登录状态和用户信息
          wx.removeStorageSync("token");
          wx.removeStorageSync("userInfo");
          wx.removeStorageSync("avatarUrl");

          // 更新页面状态
          this.setData({
            isLoggedIn: false,
            userInfo: null,
            myname: "点击登录",
            url: "/pages/index/image/header.png",
          });

          // 显示提示
          wx.showToast({
            title: "已退出登录",
            icon: "success",
          });
        }
      },
    });
  },

  onMenuItemTap: function (e) {
    // 检查是否登录
    if (!this.data.isLoggedIn) {
      wx.navigateTo({
        url: "/pages/registration/registration",
      });
      return;
    }

    const index = e.currentTarget.dataset.index;
    const item = this.data.menuItems[index];
    let url = "";
    if (item.label === "我的订单") {
      url = "/pages/myorder/myorder"; // 直接指定"我的订单"跳转路径
    } else if (item.label === "我的卡券") {
      url = "/pages/mycoupon/mycoupon"; // 修正路径，确保路径正确
    }
    if (url) {
      wx.navigateTo({
        url: url,
      });
    }
  },
});
